/*
 * Copyright © 2019-2020  Whale Cloud, Inc. All Rights Reserved.
 *
 * Notice: Whale Cloud Inc copyrights this specification.
 * No part of this specification may be reproduced in any form or means,
 * without the prior written consent of Whale Cloud Inc.
 *
 */
package com.wteam.modules.system.service.impl;

import com.wteam.modules.system.service.SelectCourseRecordService;
import com.wteam.modules.system.domain.SelectCourseRecord;
import com.wteam.modules.system.domain.dto.SelectCourseRecordDTO;
import com.wteam.modules.system.domain.criteria.SelectCourseRecordQueryCriteria;
import com.wteam.modules.system.domain.mapper.SelectCourseRecordMapper;
import com.wteam.modules.system.repository.SelectCourseRecordRepository;
import com.wteam.utils.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.*;
import lombok.RequiredArgsConstructor;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 选课记录 业务实现层.
 *
 * @author herry_wong
 * @since 2020-12-29
 */
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "selectCourseRecord")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class SelectCourseRecordServiceImpl implements SelectCourseRecordService {

    private final SelectCourseRecordRepository selectCourseRecordRepository;

    private final SelectCourseRecordMapper selectCourseRecordMapper;

    private final RedisUtils redisUtils;

    @Override
    public Map<String,Object> queryAll(SelectCourseRecordQueryCriteria criteria, Pageable pageable){
        Page<SelectCourseRecord> page = selectCourseRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) ->  QueryHelper.andPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(selectCourseRecordMapper::toDto));
    }

    @Override
    public List<SelectCourseRecordDTO> queryAll(SelectCourseRecordQueryCriteria criteria){
        return selectCourseRecordMapper.toDto(selectCourseRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) ->  QueryHelper.andPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Cacheable(key = "'id:' + #p0")
    public SelectCourseRecordDTO findDTOById(Long id) {
        SelectCourseRecord selectCourseRecord = selectCourseRecordRepository.findById(id).orElse(null);
        ValidUtil.notNull(selectCourseRecord, SelectCourseRecord.ENTITY_NAME, "id", id);
        return selectCourseRecordMapper.toDto(selectCourseRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SelectCourseRecordDTO create(SelectCourseRecord resources) {
        return selectCourseRecordMapper.toDto(selectCourseRecordRepository.save(resources));
    }

    @Override
    @CacheEvict(key = "'id:' + #p0.id")
    @Transactional(rollbackFor = Exception.class)
    public void update(SelectCourseRecord resources) {
        SelectCourseRecord selectCourseRecord = selectCourseRecordRepository.findById(resources.getId()).orElse(null);
        ValidUtil.notNull(selectCourseRecord, SelectCourseRecord.ENTITY_NAME, "id",resources.getId());

        selectCourseRecord.copy(resources);
        selectCourseRecordRepository.save(selectCourseRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(Set<Long> ids) {
        redisUtils.delByKeys("selectCourseRecord::id:", ids);
        selectCourseRecordRepository.logicDeleteInBatchById(ids);
    }


//    @RabbitListener(queues = "${spring.rabbitmq.queueName}")
//    public void receive(SelectCourseRecord selectCourseRecord) {
//        System.out.println(selectCourseRecord);
//    }

    @Override
    public void download(List<SelectCourseRecordDTO> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SelectCourseRecordDTO selectCourseRecord : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("学生id", selectCourseRecord.getStudentId());
            map.put("课程id", selectCourseRecord.getCourseId());
            map.put("创建时间", selectCourseRecord.getCreatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}